<?php
/**
* This template demonstrates how to gather comments from your users, have them
* saved to the database and list them again on the same page.
*
* In this example all comments are moderated (made visible) immediately without
* vetting.
*
* @package Scribble
* @subpackage SkeletonApp
*/
use Buan\Model;
use Buan\View;
use Buan\SystemLog;
class ScribbleTemplate_sampleCommentForm extends ScribbleTemplate implements IScribbleTemplate {

	/**
	* Gathers content from the given array and builds a StdClass object that will
	* be saved to the Page.
	*
	* @param array Content
	* @return \StdClass
	*/
	public function gatherPopulatorData(PageModel $page, $data=array()) {

		// Gather
		$data = isset($data['inner']) ? $data['inner'] : array();
		$content = new StdClass();
		$content->heading = isset($data['heading']) ? $data['heading'] : '';
		$content->content = isset($data['content']) ? $data['content'] : array();

		// Result
		return $content;
	}

	/**
	* Returns the front-end template. Also handles the POST from the comment form.
	*
	* @param PageModel Page to render
	* @return ScribblePublicView
	*/
	public function getFrontEndView(PageModel $page) {

		// Prepare the view
		$view = new ScribblePublicView();
		$view->setSource($this->getBaseFolder().'/front-end.tpl');
		$view->page = $page;
		$view->baseUrl = $this->getBaseUrl();

		// Handle POST
		if(!empty($_POST)) {

			// Gather
			$comment = Model::create('PageComment');
			$comment->addRelatives($page);
			$comment->isvisible = 1;
			$comment->authorname = $_POST['authorname'];
			$comment->authoremail = $_POST['authoremail'];
			$comment->authorwww = $_POST['authorwww'];
			$comment->body = $_POST['body'];

			// Validate
			if(!preg_match("/^[^@]+@[^\.]+\..+$/", $comment->authoremail)) {
				SystemLog::add('Please provide a valid email address', SystemLog::WARNING);
			}

			// Save
			else if(!$comment->getModelManager()->save($comment)) {
				SystemLog::add('Failed to save comment!', SystemLog::WARNING);
			}
			else {
				SystemLog::add('Comment saved!', SystemLog::INFO);
			}
		}

		// Result
		return $view;
	}

	/**
	* Returns the populator template.
	*
	* @param PageModel
	* @return ScribbleAdminView
	*/
	public function getPopulatorView(PageModel $page) {

		// Prep populator view
		$view = new ScribbleAdminView();
		$view->setSource($this->getBaseFolder().'/populator.tpl');
		$view->baseUrl = $this->getBaseUrl();
		$view->page = $page;

		// Result
		return $view;
	}

	/**
	* Returns a short description of this template.
	*
	* @return string
	*/
	public function getShortDescription() {
		return "An example of how to integrate a comments form into your template";
	}

	/**
	* Return the title of this template.
	*
	* @return string
	*/
	public function getTitle() {
		return "Sample: Comment Form";
	}

	/**
	* Return this template type.
	*
	* @return int
	*/
	public function getType() {
		return self::TYPE_INNER;
	}

	/**
	* Validate the data gathered from the input form.
	*
	* @param PageModel Page to validate
	* @param array Stores error messages
	* @return bool
	*/
	public function validatePageContent(PageModel $page, &$messages) {
		$result = TRUE;
		return $result;
	}
}
?>